package com.wavehi.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @Author WaveHi
 * @Date 13/9/2021 上午 10:52
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebServlet(urlPatterns = "/mySecondServlet", loadOnStartup = 1)
public class MySecondServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        //获取Servlet程序的别名
        System.out.println(config.getServletName());
        //获取Servlet程序的初始化参数值
//        config.getInitParameter("url");
        //获取Servlet程序的所有初始化参数名
        System.out.println(config.getInitParameterNames());
        //获取Servlet的ServletContext对象
        ServletContext servletContext = config.getServletContext();
        System.out.println(config.getServletContext());

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取Servlet配置信息对象
        ServletConfig servletConfig = getServletConfig();
        //获取Servlet上下文对象
        ServletContext servletContext = getServletContext();
        //获取Servlet初始化参数
        String username = servletContext.getInitParameter("username");
        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        System.out.println(initParameterNames);
        //获取工程路径
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //工程部署后资源的磁盘路径
        String myFirstServlet = servletContext.getRealPath("page");
        System.out.println(myFirstServlet);
        //获取或设置域对象的数据
        servletContext.setAttribute("username", "Tom");
        System.out.println(servletContext.getAttribute("username"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的资源路径
        System.out.println("URI is " + req.getRequestURI());
        //获取请求的统一资源定位符
        System.out.println("URL is " + req.getRequestURL());
        //获取客户端的ip地址
        System.out.println("Ip address is : " + req.getRemoteHost());
        //获取请求头
        System.out.println(req.getHeader("Accept"));
        //获取指定的请求参数
        System.out.println("Username is :" + req.getParameter("username"));
        //获取请求参数的多个值
        System.out.println("Parameters value of  request is : " + req.getParameterValues("password"));
        //获取请求的方式
        System.out.println("The method of data transfer is : " + req.getMethod());
        //获取或设置请求的域数据
        req.setAttribute("answer", "Ok!");
        System.out.println("The answer is : " + req.getAttribute("answer"));
        req.removeAttribute("answer");
        //获取请求转发对象
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/myFirstServlet");
        requestDispatcher.forward(req,resp);
        //获取请求体所使用的字符集
        System.out.println("The name of the character encoding used in the body of this request is : " + req.getCharacterEncoding());
        //设置请求体所使用的字符集
        req.setCharacterEncoding("UTF-8");
    }
}
